package main.java.br.com.projeto.application.ui.converters;

import br.com.caelum.stella.format.CPFFormatter;  
import javax.faces.component.UIComponent;  
import javax.faces.context.FacesContext;  
import javax.faces.convert.Converter;  
import javax.faces.convert.ConverterException;  
import javax.faces.convert.FacesConverter;  
  
@FacesConverter("cpfConverter")  
public class CPFConverter implements Converter {  
  
  @Override  
  public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)  
          throws ConverterException {  
    return arg2;  
  }  
  
  @Override  
  public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)  
          throws ConverterException {  
    if (arg2 == null) {  
      return "";  
    }  
    CPFFormatter f = new CPFFormatter();  
    return f.format(arg2.toString());  
  }  
  
  public static String formataCPF(String cpf) {  
    if (cpf == null || "".equals(cpf)) {  
      return "";  
    }  
    return new CPFFormatter().format(cpf);  
  }  
}